package com.xhwl.centiotdata.converter.vehicle;


import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.dto.fushi.VehiclePayRecordFuShiDTO;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleParkingInfoBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.PayRecordBO;
import com.xhwl.common.utils.ChkUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用于将第三方的出行记录实体类转换为中台熟知的实体类
 */
@Component
public class VehiclePayRecordThirdConverter {
    /**
     * 转换富士支付记录List
     */
    public List<PayRecordBO> fuShiList2PayRecordBOList(List<VehiclePayRecordFuShiDTO> vehiclePayRecordFuShiDTOS, VehicleIntegrationConfigBO vehicleIntegrationConfigBO) {
        List<String> outAutoPlateList = vehiclePayRecordFuShiDTOS.stream().map(t -> t.getTokenId()).collect(Collectors.toList());
        VehicleParkingInfoBO vehicleParkingInfoBO = vehicleIntegrationConfigBO.getVehicleParkingInfoBO();
        String host = vehicleParkingInfoBO.getHost();

        Map<String, String> colorMap = new HashMap<>();
        if(ChkUtil.isNotNull(outAutoPlateList)) {
            StringBuilder stringBuilder = new StringBuilder();
            for(String s : outAutoPlateList) {
                stringBuilder.append("'").append(s).append("',");
            }
            String params = stringBuilder.substring(0, stringBuilder.length() - 1);
            params = "OutAutoPlate in (" + params + ")";
            String str = HttpRequest.post(host + "/OutVehicle/GetByFunc").header("Content-Type", "application/json").body(JSONObject.toJSONString(params)).execute().body();
            JSONObject result = JSON.parseObject(str);
            JSONArray models = result.getJSONArray("Models");
            if(ChkUtil.isNotNull(models)) {
                List<Map> colorList = models.toJavaList(Map.class);
                colorMap = colorList.stream().collect(Collectors.toMap(t->String.valueOf(t.get("TokenNo")), t->String.valueOf(t.get("PlateColor")), (v1,v2)->v2));
            }
        }
        Map<String, String> finalColorMap = colorMap;
        return vehiclePayRecordFuShiDTOS.stream().map(s -> fuShi2PayRecordForInBO(s, vehicleIntegrationConfigBO, finalColorMap)).collect(Collectors.toList());
    }


    /**
     * 富士进记录转换为AccessRecordForInBO
     */
    public PayRecordBO fuShi2PayRecordForInBO(VehiclePayRecordFuShiDTO vehiclePayRecordFuShiDTO, VehicleIntegrationConfigBO vehicleIntegrationConfigBO, Map<String, String> colorMap) {
        PayRecordBO payRecordBO = new PayRecordBO();
        if (vehiclePayRecordFuShiDTO != null) {
            payRecordBO.setPlateNumber(vehiclePayRecordFuShiDTO.getTokenId());
            payRecordBO.setPayRecordId(vehiclePayRecordFuShiDTO.getID());
            payRecordBO.setEnterTime(DateUtil.parseDateTime(vehiclePayRecordFuShiDTO.getInTime().replace("T", " ")));
            payRecordBO.setPayTime(DateUtil.parseDateTime(vehiclePayRecordFuShiDTO.getTollTime().replace("T", " ")));
            payRecordBO.setPaidMoney(vehiclePayRecordFuShiDTO.getCashAmount() * 100);
            payRecordBO.setTotalMoney(vehiclePayRecordFuShiDTO.getTranAmount() * 100);
            payRecordBO.setPayMethod(converterFromFuShi(vehiclePayRecordFuShiDTO.getTollType()));
            payRecordBO.setParkId(vehicleIntegrationConfigBO.getVehicleParkingInfoBO().getParkId());
            payRecordBO.setDeductedHours(vehiclePayRecordFuShiDTO.getDeductedHours() * 3600);
            payRecordBO.setDeductedAmount(vehiclePayRecordFuShiDTO.getDeductedAmount() * 100);
            payRecordBO.setTranId(vehiclePayRecordFuShiDTO.getTranId());
            payRecordBO.setPlateColor(colorMap.get(vehiclePayRecordFuShiDTO.getTokenId()));
        }
        payRecordBO.setProjectId(vehicleIntegrationConfigBO.getVehicleSystemInfoBO().getProjectId());
        return payRecordBO;
    }

    public static Integer converterFromFuShi(Integer tollType) {
//        支付方式（0 混合多种方式, 1 银联闪付, 2 微信, 3 现金, 4 ETC, 5 支付宝, 6 银联钱包, 7 翼支付, 8 百度钱包, 9 POS机，10 优惠券，11 会员积分,  12 其他）
        Integer result;
        switch (tollType) {
            case 0:
                result = 1016;
                break;
            case 1:
                result = 1002;
                break;
            case 2:
                result = 1004;
                break;
            case 3:
                result = 1001;
                break;
            case 4:
                result = 1008;
                break;
            case 5:
                result = 1005;
                break;
            case 6:
                result = 1007;
                break;
            case 7:
                result = 1013;
                break;
            case 8:
                result = 1014;
                break;
            case 9:
                result = 1015;
                break;
            case 10:
                result = 1010;
                break;
            case 11:
                result = 1012;
                break;
            case 12:
                result = 1000;
                break;
            default:
                result = 7;

        }
        return result;
    }
}
